package com.rdtravel.converters;

import com.rdtravel.entities.Continent;
import com.rdtravel.services.ContinentsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;

public class StringToContinentConverter implements Converter<String, Continent> {
	private final ContinentsService continentsService;

	@Autowired
	public StringToContinentConverter(ContinentsService continentsService) {
		this.continentsService = continentsService;
	}

	@Override
	public Continent convert(String id) {
		if (id == null || id.isEmpty()) {
			return null;
		}
		try {
			return continentsService.findById(Long.parseLong(id));
		} catch (NumberFormatException ex) {
			return null;
		}
	}
}
